<template>
  <div class="login">
    <div class="body-wrapper">
      <div class="bg1"></div>
      <div class="gyl">
        宝田升-代办清单
        <div class="gy2">项目代办清单管理系统</div>
      </div>
      <div class="bg">
        <div class="form">
          <Form
              ref="loginForm"
              :model="form"
              :rules="rules"
              class="login-form"
              mode="block">
            <template v-if="organization">
              <div class="wel">选择数据视角</div>
              <div class="organization-list">
                <ul class="organization">
                  <li @click="changeView(null)" class="organization-merchant">
                    [{{ organization.merchant.code }}] {{ organization.merchant.name }}
                    <i v-if="!selected" class="fa fa-check-circle green-color"></i>
                  </li>
                  <ul class="organization-item">
                    <dt @click="changeView(o.id)" v-for="o in organization.organizationList">
                      [{{ o.code }}] {{ o.name }}
                      <i v-if="selected === o.id" class="fa fa-check-circle green-color"></i>
                    </dt>
                  </ul>
                </ul>
              </div>
              <FormItem :showLabel="false">
                <Button :disabled="account.role.organizationId && !selected" :loading="loading" class="login-form-btn" color="green" @click="doChangeView">确认选择</Button>
              </FormItem>
            </template>
            <template v-else>
              <div class="wel">商户中心</div>
              <FormItem label="用户名" prop="username">
                <template v-slot:label> 账号</template>
                <Input type="text" v-model="form.username" autocomplete="off" placeholder="请输入登录账号"/>
              </FormItem>
              <FormItem label="密码" prop="password">
                <Input type="password" v-model="form.password" autocomplete="off" @keyup.enter="submitForm" placeholder="请输入密码"/>
              </FormItem>
              <FormItem :showLabel="false">
                <Button :loading="loading" class="login-form-btn" color="primary" @click="submitForm">登 录</Button>
              </FormItem>
              联系电话：13456781004（同微信）
            </template>
          </Form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

import {ChangeView, Login} from "@js/api/Common";
import {loading, message} from "heyui";
/**
 * @功能描述: 登录
 * @创建时间: 2023年08月08日
 * @公司官网: www.fenxi365.com
 * @公司信息: 纷析云（杭州）科技有限公司
 * @公司介绍: 专注于财务相关软件开发, 企业会计自动化解决方案
 */
export default {
  name: "Login",
  data() {
    return {
      loading: false,
      organization: null,
      account: null,
      selected: null,
      form: {
        username: null,
        password: null
      },
      rules: {
        required: ['username', 'password']
      }
    }
  },

  methods: {
    submitForm() {
      let validResult = this.$refs.loginForm.valid();
      if (validResult.result) {
        this.loading = true;
        Login(this.form).then(({success, data: {organization, account}}) => {
          if (success) {
            message("登录成功~");
            this.organization = organization;
            this.account = account;
            localStorage.setItem("m_cache_username", this.form.username)
          }
        }).finally(() => {
          this.loading = false;
        });
      }
    },
    changeView(id) {
      if (this.account.role.organizationId && !id) return;
      this.selected = id;
    },
    doChangeView() {
      loading("操作中....");
      ChangeView(this.selected).then(() => {
        message("设置成功~");
        window.location.replace("/");
      }).finally(() => loading.close())
    }
  },
  created() {
    let username = localStorage.getItem("m_cache_username");
    if (username) {
      this.form.username = username;
    }
  }
}
</script>
<style scoped lang="less">

.login {
  background: url("@/assets/login-bg.jpg") no-repeat;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background-size: cover;
  background-position: center;

  &-form {
    margin: 20px 20px 20px;

    &-btn {
      width: 100%;
      margin-top: 10px;
    }

    &-action {
      width: 100%;
      font-size: 12px;
      color: @primary-color;
      cursor: pointer;
      display: flex;
      justify-content: space-between;
    }
  }

  .login-form {
    .login-form-btn {
      height: 40px;
    }
  }


  .img {
    width: 100%;
    position: absolute;
    left: 0;
    right: 0;
    margin: 0 auto;
    z-index: -1;
  }

  .wel {
    color: @primary-color;
    font-weight: bold;
    letter-spacing: 5px;
    font-size: 1.5rem;
    margin-bottom: 10px;
    text-align: center;
  }

  .bg1 {
    width: 100%;
    height: 237px;
    background: @primary-color;
    opacity: 0.6;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
  }

  .bg {
    position: absolute;
    left: 56%;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    z-index: 1;
    border-radius: 5px;
    display: flex;
    justify-content: center;
    align-items: center;

    .form {
      width: 400px;
      background: #fff;
      box-shadow: 0 0 50px rgba(0, 0, 0, 0.4);
    }
  }

  .gyl {
    width: 530px;
    height: 237px;
    color: #FFFFFF;
    font-size: 65px;
    position: absolute;
    left: 15%;
    top: 11%;
    bottom: 0;
    margin: auto;
  }

  .gy2 {
    color: #fff;
    margin-left: 6px;
    font-size: 18px;
    text-align: center;
    margin-top: 10px;
  }

  .btn {
    position: absolute;
    top: 16rem;
    right: 2.5rem;
    border: none;
    color: #fff;
    width: 18.825rem;
    text-align: center;
    text-indent: 0;
  }
}

.organization {

  &-list {
    margin: 20px 0;
    max-height: 400px;
    overflow-y: auto;
  }

  min-width: 150px;

  li, dt {
    cursor: pointer;
    padding: 5px 10px;

    &:hover {
      background-color: @gray3-color;
      color: @primary-color;
    }
  }

  dt {
    padding-left: 30px;
  }
}
</style>
